package com.password.desktop.UI;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

import javax.swing.border.Border;

public class PMLineBorder implements Border {

	private BorderParam top = new BorderParam();
	private BorderParam left = new BorderParam();
	private BorderParam right = new BorderParam();
	private BorderParam bottom = new BorderParam();

	public static PMLineBorder builder(int width, Color color) {
		return new PMLineBorder().bottom(new BorderParam(width, color)).top(new BorderParam(width, color))
				.left(new BorderParam(width, color)).right(new BorderParam(width, color));
	}

	public static PMLineBorder builder() {
		return new PMLineBorder();
	}

	public BorderParam getTop() {
		return top;
	}

	public PMLineBorder top(BorderParam top) {
		this.top = top;
		return this;
	}

	public BorderParam getLeft() {
		return left;
	}

	public PMLineBorder left(BorderParam left) {
		this.left = left;
		return this;
	}

	public BorderParam getRight() {
		return right;
	}

	public PMLineBorder right(BorderParam right) {
		this.right = right;
		return this;
	}

	public BorderParam getBottom() {
		return bottom;
	}

	public PMLineBorder bottom(BorderParam bottom) {
		this.bottom = bottom;
		return this;
	}

	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Graphics2D g2 = (Graphics2D) g;
		if (top != null) {
			g2.setStroke(new BasicStroke(top.width));
			g.setColor(top.color);
			g.drawLine(0, 0, width, 0);
		}
		if (left != null) {
			g2.setStroke(new BasicStroke(left.width));
			g.setColor(left.color);
			g.drawLine(0, 0, 0, height);
		}
		if (bottom != null) {
			g2.setStroke(new BasicStroke(bottom.width));
			g2.setColor(bottom.color);
			g2.drawLine(0, height - bottom.width, width, height - bottom.width);
		}
		if (right != null) {
			g2.setStroke(new BasicStroke(right.width));
			g.setColor(right.color);
			g.drawLine(width - right.width, 0, width - right.width, height);
		}

	}

	@Override
	public Insets getBorderInsets(Component c) {
		return new Insets(0, 0, 0, 0);
	}

	@Override
	public boolean isBorderOpaque() {
		return true;
	}

	public static class BorderParam {
		Color color = new Color(239, 239, 239);
		int width = 1;

		public BorderParam(int width, Color color) {
			this.color = color;
			this.width = width;
		}

		public BorderParam() {
			super();
		}

		public Color getColor() {
			return color;
		}

		public void setColor(Color color) {
			this.color = color;
		}

		public int getWidth() {
			return width;
		}

		public void setWidth(int width) {
			this.width = width;
		}

	}

}
